package com.ruoyi.standingbook.toilet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.standingbook.recycle.domain.RecycleImportBase;
import com.ruoyi.standingbook.toilet.domain.ToiletImportDetail;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.toilet.domain.bo.ToiletImportBaseBo;
import com.ruoyi.standingbook.toilet.domain.vo.ToiletImportBaseVo;
import com.ruoyi.standingbook.toilet.domain.ToiletImportBase;
import com.ruoyi.standingbook.toilet.mapper.ToiletImportBaseMapper;
import com.ruoyi.standingbook.toilet.service.IToiletImportBaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 市政公厕管养导入Service业务层处理
 *
 * @author cc
 * @date 2023-04-30
 */
@RequiredArgsConstructor
@Service
public class ToiletImportBaseServiceImpl implements IToiletImportBaseService {

    private final ToiletImportBaseMapper baseMapper;

    /**
     * 查询市政公厕管养导入
     */
    @Override
    public ToiletImportBaseVo queryById(String batchNo){
        return baseMapper.selectVoById(batchNo);
    }

    /**
     * 查询市政公厕管养导入列表
     */
    @Override
    public TableDataInfo<ToiletImportBaseVo> queryPageList(ToiletImportBaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ToiletImportBase> lqw = buildQueryWrapper(bo);
        Page<ToiletImportBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询市政公厕管养导入列表
     */
    @Override
    public List<ToiletImportBaseVo> queryList(ToiletImportBaseBo bo) {
        LambdaQueryWrapper<ToiletImportBase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ToiletImportBase> buildQueryWrapper(ToiletImportBaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ToiletImportBase> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), ToiletImportBase::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProcessStatus() != null, ToiletImportBase::getProcessStatus, bo.getProcessStatus());
        lqw.eq(bo.getAttachMsg() != null, ToiletImportBase::getAttachMsg, bo.getAttachMsg());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ToiletImportBase::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(ToiletImportBase::getCreateTime);
        return lqw;
    }

    /**
     * 新增市政公厕管养导入
     */
    @Override
    public Boolean insertByBo(ToiletImportBaseBo bo) {
        ToiletImportBase add = BeanUtil.toBean(bo, ToiletImportBase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBatchNo(add.getBatchNo());
        }
        return flag;
    }

    /**
     * 修改市政公厕管养导入
     */
    @Override
    public Boolean updateByBo(ToiletImportBaseBo bo) {
        ToiletImportBase update = BeanUtil.toBean(bo, ToiletImportBase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ToiletImportBase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除市政公厕管养导入
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
